package com.ecloud.support.generator.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ecloud.common.base.result.Result;
import com.ecloud.common.base.result.ResultBuilder;
import com.ecloud.common.base.result.ResultStatus;
import com.ecloud.common.base.result.RowsBuilder;
import com.ecloud.common.utils.check.LongUtils;
import com.ecloud.common.utils.check.StringUtils;
import com.ecloud.common.utils.code.UUIDUtil;
import com.ecloud.support.generator.mapper.TemplateMapper;
import com.ecloud.support.generator.mapper.TemplateSetMapper;
import com.ecloud.support.generator.model.convert.TemplateSetConverter;
import com.ecloud.support.generator.model.dto.TemplateSetDto;
import com.ecloud.support.generator.model.entity.TemplatePo;
import com.ecloud.support.generator.model.entity.TemplateSetPo;
import com.ecloud.support.generator.model.vo.TemplateSetVo;
import com.ecloud.support.generator.service.iface.ITemplateSetService;

/**
 * TemplateSetSetService
 *
 * @author LiuGangQiang Create in 2021/04/15
 */
@Service
public class TemplateSetService extends ServiceImpl<TemplateSetMapper, TemplateSetPo> implements ITemplateSetService {
    @Resource
    private TemplateMapper templateMapper;

    /**
     * @see com.ecloud.support.generator.service.iface.ITemplateSetService#insert(com.ecloud.support.generator.model.dto.TemplateSetDto)
     */
    @Override
    public Result<?> insert(TemplateSetDto dto) {
        /* 判断数据库是否存在记录 */
        LambdaQueryWrapper<TemplateSetPo> queryWrapper = new QueryWrapper<TemplateSetPo>().lambda();
        queryWrapper.select(TemplateSetPo::getId);
        queryWrapper.eq(TemplateSetPo::getSetName, dto.getSetName());
        if (this.getOne(queryWrapper) != null) {
            return ResultBuilder.build(ResultStatus.CONFLICT).setKey("templateset.name.conflict", dto.getSetName());
        }

        /* 不存在执行新增 */
        dto.setId(UUIDUtil.uuid());
        dto.setCreateTime(LocalDateTime.now());
        TemplateSetPo po = TemplateSetConverter.INSTANCE.dtoToPo(dto);
        if (this.save(po)) {
            return ResultBuilder.build(ResultStatus.OK);
        }
        return ResultBuilder.build(ResultStatus.FAIL);
    }

    /**
     * @see com.ecloud.support.generator.service.iface.ITemplateSetService#delete(com.ecloud.support.generator.model.dto.TemplateSetDto)
     */
    @Override
    public Result<?> delete(TemplateSetDto dto) {
        /* 判断数据库是否存在记录 */
        LambdaQueryWrapper<TemplateSetPo> queryWrapper = new QueryWrapper<TemplateSetPo>().lambda();
        queryWrapper.eq(TemplateSetPo::getId, dto.getId());
        queryWrapper.select(TemplateSetPo::getId);
        if (this.getOne(queryWrapper) == null) {
            return ResultBuilder.build(ResultStatus.DATA_NOT_FOUND).setKey("templateset.id.not.found", dto.getId());
        }
        /* 存在执行删除 */
        if (this.remove(queryWrapper)) {
            /* 删除对应模板 */
            LambdaQueryWrapper<TemplatePo> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(TemplatePo::getTemplateSetId, dto.getId());
            templateMapper.delete(wrapper);
            return ResultBuilder.build(ResultStatus.OK);
        }
        return ResultBuilder.build(ResultStatus.FAIL);
    }

    /**
     * @see com.ecloud.support.generator.service.iface.ITemplateSetService#update(com.ecloud.support.generator.model.dto.TemplateSetDto)
     */
    @Override
    public Result<?> update(TemplateSetDto dto) {
        /* 判断数据库是否存在记录 */
        LambdaQueryWrapper<TemplateSetPo> queryWrapper = new QueryWrapper<TemplateSetPo>().lambda();
        queryWrapper.select(TemplateSetPo::getId);
        queryWrapper.eq(TemplateSetPo::getId, dto.getId());
        if (this.getOne(queryWrapper) == null) {
            return ResultBuilder.build(ResultStatus.DATA_NOT_FOUND).setKey("templateset.id.not.found", dto.getId());
        }

        /* 判断修改的数据是否存在冲突 */
        LambdaQueryWrapper<TemplateSetPo> uniqueWrapper = new QueryWrapper<TemplateSetPo>().lambda();
        uniqueWrapper.ne(TemplateSetPo::getId, dto.getId());
        uniqueWrapper.eq(TemplateSetPo::getSetName, dto.getSetName());
        queryWrapper.select(TemplateSetPo::getId);
        if (this.getOne(uniqueWrapper) != null) {
            return ResultBuilder.build(ResultStatus.CONFLICT).setKey("templateset.name.conflict", dto.getSetName());
        }

        /* 修改数据 */
        LambdaUpdateWrapper<TemplateSetPo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(TemplateSetPo::getId, dto.getId());
        /* 存在执行修改 */
        TemplateSetPo po = TemplateSetConverter.INSTANCE.dtoToPo(dto);
        if (this.update(po, updateWrapper)) {
            return ResultBuilder.build(ResultStatus.OK);
        }
        return ResultBuilder.build(ResultStatus.FAIL);
    }

    /**
     * @see com.ecloud.support.generator.service.iface.ITemplateSetService#queryList(com.ecloud.support.generator.model.dto.TemplateSetDto)
     */
    @Override
    public Result<?> queryList(TemplateSetDto dto) {
        /* 构造查询条件 */
        LambdaQueryWrapper<TemplateSetPo> queryWrapper = new QueryWrapper<TemplateSetPo>().lambda();
        queryWrapper.and(StringUtils.isNotBlank(dto.getKeyword()),
                wrapper -> wrapper.like(TemplateSetPo::getSetName, dto.getKeyword()).or().like(TemplateSetPo::getDesc, dto.getKeyword()));
        queryWrapper.orderByAsc(TemplateSetPo::getCreateTime);
        /* 构造分页查询 */
        IPage<TemplateSetPo> page = this.page(new Page<>(dto.getPage(), dto.getRows()), queryWrapper);
        if (LongUtils.isGtZero(page.getTotal())) {
            /* 转换 */
            List<TemplateSetVo> pos = TemplateSetConverter.INSTANCE.poToVo(page.getRecords());
            return ResultBuilder.build(ResultStatus.OK, RowsBuilder.build(page.getTotal(), pos));
        }
        return ResultBuilder.build(ResultStatus.DATA_NOT_FOUND, RowsBuilder.build());
    }

}
